package fr.enseirbmatmeca.apms.tapestry.pages;

import java.util.List;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.Request;

import fr.enseirbmatmeca.apms.spring.model.project.ProjectCollection;
import fr.enseirbmatmeca.apms.spring.security.ConstantRoleUserService;
import fr.enseirbmatmeca.apms.spring.security.ProjectRole;
import fr.enseirbmatmeca.apms.spring.service.ProjectManager;
import fr.enseirbmatmeca.apms.spring.service.UserManager;

public class DetailProject extends Security{

	@Inject
	protected Request request;
	@Inject
	private PageRenderLinkSource prls; 

	private String errorMessage;

	@Inject
	private ProjectManager projectManager;


	Object onActivate() {
		String role = getRole();
		String idProject = (String) request.getParameter("id");

		if (idProject==null){
			errorMessage="Invalid id. Ex : detailproject?id=...";
			return null;
		}
		
		List<ProjectRole> listProjectRole = getListProjectRole(idProject);

		if (role==null)
			return Index.class;	


		else if (role.equals(ConstantRoleUserService.ADMINISTRATOR)) {
			Link link = prls.createPageRenderLink(fr.enseirbmatmeca.apms.tapestry.pages.administrator.DetailProject.class);
			link.addParameter("id", idProject);
			return link;
		}

		else if (role.equals(ConstantRoleUserService.USER)){
			if (listProjectRole==null){
				errorMessage="Project list role server error";
				return null;
			}

			if(listProjectRole.size() == 0){
				//VISITOR
				Link link = prls.createPageRenderLink(fr.enseirbmatmeca.apms.tapestry.pages.visitor.DetailProject.class);
				link.addParameter("id", idProject);
				return link;
			}

			Link link = prls.createPageRenderLink(Index.class);
			for (ProjectRole projectRole : listProjectRole)
				if(projectRole.getProjectCollectionId() != null){
					//it's a tutor ( see DAO implementation )
					// we check if project is inside a project collection he manages
					boolean check = projectManager.checkProjectIntoProjectCollection(idProject, projectRole.getProjectCollectionId());
					if(check){
						link = prls.createPageRenderLink(fr.enseirbmatmeca.apms.tapestry.pages.tutor.DetailProject.class);
						link.addParameter("id", idProject);
					}
				}

				else if (projectRole.getProjectGroupId() != null){

					//it's a reporter or a student ( see DAO implementation )
					// we check if project is inside a project group he is part of
					boolean check = projectManager.checkProjectIntoProjectGroup(idProject, projectRole.getProjectGroupId());
					if(check){

						if (projectRole.getAuthority().equals(ProjectRole.AUTHORITY_REPORTER))
							link =  prls.createPageRenderLink(fr.enseirbmatmeca.apms.tapestry.pages.reporter.DetailProject.class);

						else if (projectRole.getAuthority().equals(ProjectRole.AUTHORITY_STUDENT))	
							link =  prls.createPageRenderLink(fr.enseirbmatmeca.apms.tapestry.pages.student.DetailProject.class);
						
						link.addParameter("id", idProject);
						link.addParameter("group", projectRole.getProjectGroupId());
					}

				}

			return link;

		}

		return Index.class;	

	} 

	@CleanupRender
	public void cleanup() {
		this.errorMessage = null;
	}

	public String getErrorMessage() {
		return this.errorMessage;
	}

}
